<?php
namespace App\Controllers\Classc;

use App\Libs\Views\Json;
use App\Models\ClassModel\ClassModel;
class ClassRecom extends \App\Libs\Base\BaseAction
{

    /*
     * 推荐悬赏班，体验班到班级首页
     *
     * class_type:  //班级类型
     * operation:  //操作类型：up:设置推荐，down：取消推荐；
     * class_id:  //班级id
     */

    public function execute()
    {

        if(!$this->request->hasQuery('class_type') || !$this->request->hasQuery('operation') || !$this->request->hasQuery('class_id')){
            return Json::view(1,'缺少参数',[]);
        }
        $class_type = $this->request->getQuery('class_type');
        if(!in_array($class_type,['2','3'])){
            return Json::view(1,'不可操作的班级类型',[]);
        }
        $operation = $this->request->getQuery('operation');
        $class_id = $this->request->getQuery('class_id');

        $matchClass = ClassModel::findOne([
            'id=?1 and class_type=?2',
            'bind' => [1=>$class_id,2=>$class_type]
        ]);
        if(empty($matchClass)){
            return Json::view(1,'指定类型的班级不存在',[]);
        }


        switch ($operation){
            case 'down':
                $recomNum = ClassModel::count([
                    'conditions' =>'class_type=?1 and class_recommend=?2',
                    'bind' =>[1=>$class_type,2=>'2']
                ]);
                if($recomNum <= 2){
                    return Json::view(1,'错误：请先设置两个以上推荐后再次操作',[]);
                }

                $matchClass->class_recommend = '1';//下来
                if($matchClass->save()){
                    return Json::view(0,'操作成功',[]);
                }else{
                    return Json::view(1,'操作失败',[]);
                }
                break;
            case 'up':
                $matchClass->class_recommend = '2';//下来
                if($matchClass->save()){
                    return Json::view(0,'操作成功',[]);
                }else{
                    return Json::view(1,'操作失败',[]);
                }
                break;
        }







    }
}
